<?php

class Model_Class extends Model_Pandrade {

	const TABLE = "class";
	
	/**
	 * @var protected $id id value
	 * @method int get_id() get_id(void)
	 * @method void set_id() set_id(int $id)
	 */
	protected $id;
	
	/**
	 * @var protected $course_id course_id value
	 * @method int get_course_id() get_course_id(void)
	 * @method void set_course_id() set_course_id(int $course_id)
	 */
	protected $course_id;
	
	/**
	 * @var protected $teacher teacher value
	 * @method string get_teacher() get_teacher(void)
	 * @method void set_teacher() set_teacher(string $teacher)
	 */
	protected $teacher;
	
	/**
	 * @var protected $identification identification value
	 * @method string get_identification() get_identification(void)
	 * @method void set_identification() set_identification(string $identification)
	 */
	protected $identification;
	
	/**
	 * @var protected $priority priority value
	 * @method string get_priority() get_priority(void)
	 * @method void set_priority() set_priority(string $priority)
	 */
	protected $priority="3";
        
	protected $class_times=array();
	/**
	 * @return Model_Class
	 */
	public function __construct($id=0) {
		if(0<intval($id)) {
			$db = DB::select("*")->from(self::TABLE)->where("id", "=", $id)->limit(1);
			$db = $db->as_assoc()->execute();
			if($db->count()>0) {
				$this->load($db->current());
                                $this->class_hours = static::get_class_times($this->id);
			}
		}
	}

	protected static function get_database_fields() {
		return array(
			'course_id' => __("Class_Course_id"),
			'teacher' => __("Class_Teacher"),
			'identification' => __("Class_Identification"),
			'priority' => __("Class_Priority"),
		);
	}

	public function validate() {
		$validateArray = array();
		foreach (self::get_database_fields() as $key => $value) {
			$validateArray[$key] = $this->get($key);
		}
		$validation = Validation::factory($validateArray)
				->rule('course_id', 'not_empty')
				->rule('identification', 'not_empty')
				->rule('priority', 'not_empty')
				;
		if ($validation->check()) {
			return $validateArray;
		} else {
			throw new Validation_Exception('validation', $validation);
		}
	}

	public static function get_report() {
		return DB::select(
				"id"
				, "course_id"
				, "teacher"
				, "identification"
				, "priority"
				, array("id", "actions"))
		->from(self::TABLE)
		->order_by("id", "ASC");
	}
        public static function get_course_classes($course_id, $only_ids=false){
            $classes = DB::select("id")->from("class")->where("course_id","=",$course_id)->order_by("id","DESC")->execute()->as_array() ;

            if($only_ids){
                return $classes;
            }else{
                foreach($classes as $i=>$class){
                    $classes[$i] = new Model_Class($class['id']);
                }
                return $classes;
            }
        }
        public static function get_class_times($class_id, $translate_week_days = true){
            $times = DB::select('*')->from("class_hours")->where("class_id","=",$class_id)->execute()->as_array();
            if($translate_week_days){
                foreach($times as $i=>$time){
                    $times[$i]["week_day"] = ucfirst(__($time['week_day']));
                }
            }
            return $times;
        }
        public function set_class_time($week_day, $start_time, $end_time){
            $value=array(
                "class_id"=>  $this->get_id(),
                "week_day"=> $week_day,
                "start_time"=> $start_time,
                "end_time"=> $end_time
                );
            DB::insert("class_hours",array_keys($value))->values(array_values($value))->execute();
            $this->class_times[] = $value;
        }
        public function get_times($translate_week_days = true){
            return Model_Class::get_class_times($this->id, $translate_week_days);
        }
        public static function load_class_as_assoc($class_id, $user_id, $translate_week_days=false){
            $class = DB::select("class.id","class.teacher","class.identification","class.priority")->from("class")
                    ->join("course")->on("course.id", "=", "class.course_id")
                    ->where("class.id","=",$class_id)->and_where("course.user_id","=",$user_id)->execute()->current();
            $class['times'] = Model_Class::get_class_times($class_id,$translate_week_days);
            return $class;
        }
        public function to_array(){
            $return = array();
            $return['id'] = $this->id;
            $return['course_id'] = $this->course_id;
            $return['teacher'] = $this->teacher;
            $return['identification'] = $this->identification;
            $return['priority'] = $this->priority;
            $return['times'] = $this->get_times();
            return $return;
        }
        public function remove_all_class_times(){
            DB::delete("class_hours")->where("class_id", "=", $this->id)->execute();
        }
        public function remove(){
            $this->remove_all_class_times();
            parent::remove();
        }
}